package com.wbao.dianniu.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;

import com.tencent.imsdk.TIMCallBack;
import com.tencent.qcloud.timchat.utils.LoginBusiness;
import com.wbao.dianniu.R;
import com.wbao.dianniu.customView.NoDoubleClickListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.logical.GlobalUserData;

/**
 * Created by yejianjun on 2018/9/8.
 */

public class ActDialog extends Activity {
    private final String TAG = ActDialog.class.getSimpleName();
    private Button existBtn;
    private Button reLoginBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_dialog);
        initView();

    }

    private void initView() {
        existBtn = (Button) findViewById(R.id.exist_btn);
        reLoginBtn = (Button) findViewById(R.id.relogin_btn);
        existBtn.setOnClickListener(noDoubleClickListener);
        reLoginBtn.setOnClickListener(noDoubleClickListener);
    }

    NoDoubleClickListener noDoubleClickListener = new NoDoubleClickListener() {
        @Override
        public void onNoDoubleClick(View v) {
            switch (v.getId()) {
                case R.id.exist_btn:
                    GlobalContext.cleanCacheToken();
                    Intent intent = new Intent();
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.setClass(ActDialog.this, LoginActivity.class);
                    startActivity(intent);
                    dismiss();
                    break;
                case R.id.relogin_btn:
                    reginLogin();
                    dismiss();
                    break;

                default:
                    break;
            }
        }
    };

    /**
     * 重新登录
     */
    private void reginLogin() {
        GlobalUserData userData = new GlobalUserData();
        LoginBusiness.loginIm(userData.getEnvironemt() + GlobalContext.getAccountId(), userData.getUserSig(), new TIMCallBack() {
            @Override
            public void onError(int i, String s) {
                Log.e(TAG, "onError..." + i + "  s:" + s);
            }

            @Override
            public void onSuccess() {
                Log.e(TAG, " region login onSuccess...");
            }
        });
    }

    private void dismiss() {
        this.finish();
    }


    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) { // 禁用返回建
            //do something.
            return true;
        } else {
            return super.dispatchKeyEvent(event);
        }
    }
}
